/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Pantalla4.java
 *
 * Created on 19/02/2011, 06:42:06 PM
 */

package pantallas;

import clases.Profesor;
import clases.Seccion;

import java.awt.Dialog.ModalExclusionType;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;


public class Pantalla1 extends javax.swing.JFrame {

   private Seccion seccion;
     private Vector<JTextField> textos;


    public void setSeccion(Seccion seccion) {
        this.seccion = seccion;
        txtnumero.setText(""+seccion.getNumseccion());
    }

 
    public Pantalla1() {
        initComponents();
        textos = new Vector<JTextField>();
        textos.add(txtapellido);
        textos.add(txtcedula);
        textos.add(txtnombre);
        textos.add(txtnumero);
        textos.add(txttitulo);
    }

        public void reiniciar(){
         for (int i=0; i<textos.size(); i++)
            textos.elementAt(i).setText("");
    }

        public void mostrarPromedio(){
            lblpromedio.setText(""+seccion.promedio());
            txtapellido.setText(seccion.getProf().getApellido());
            txtcedula.setText(""+seccion.getProf().getCedula());
            txtnombre.setText(seccion.getProf().getNombre());
            txttitulo.setText(seccion.getProf().getTitulo());
            txtapellido.setEnabled(false);
            txtcedula.setEnabled(false);
            txtnombre.setEnabled(false);
            txttitulo.setEnabled(false);
        }

         public boolean validarVacios(){
           for (int i=0; i<textos.size(); i++)
              if(textos.elementAt(i).getText().equals(""))
                return false;
           return true;
   }

   public boolean validarCedula(){
        try {
            if(Long.parseLong(txtcedula.getText())>0)
                return true;
            else
                return false;
	} catch (NumberFormatException e) {
            return false;
        }
   }

      public boolean validarNumero(){
        try {
            if(Integer.parseInt(txtnumero.getText())>0)
                return true;
            else
                return false;
	} catch (NumberFormatException e) {
            return false;
        }
   }

    
    @SuppressWarnings("unchecked")
    
    private void initComponents() {

        jInternalFrame1 = new javax.swing.JInternalFrame();
        btnaceptar = new javax.swing.JButton();
        lblpromedio = new java.awt.Label();
        btnsalir = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtcedula = new javax.swing.JTextField();
        txtnombre = new javax.swing.JTextField();
        txtapellido = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txttitulo = new javax.swing.JTextField();
        label1 = new java.awt.Label();
        btncancelar = new javax.swing.JButton();
        label2 = new java.awt.Label();
        txtnumero = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jInternalFrame1.setTitle("Seccion");
        jInternalFrame1.setVisible(true);
        jInternalFrame1.getContentPane().setLayout(null);

        btnaceptar.setText("Aceptar");
        btnaceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnaceptarActionPerformed(evt);
            }
        });
        jInternalFrame1.getContentPane().add(btnaceptar);
        btnaceptar.setBounds(30, 340, 130, 30);
        jInternalFrame1.getContentPane().add(lblpromedio);
        lblpromedio.setBounds(170, 70, 140, 30);

        btnsalir.setText("Salir");
        btnsalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnsalirActionPerformed(evt);
            }
        });
        jInternalFrame1.getContentPane().add(btnsalir);
        btnsalir.setBounds(310, 340, 110, 30);

        jPanel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel1.setToolTipText("Profesor");
        jPanel1.setName("Profesor"); // NOI18N

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel1.setForeground(new java.awt.Color(0, 102, 255));
        jLabel1.setText("Profesor");

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel2.setText("Nombre");

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel3.setText("Apellido");

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel4.setText("Cedula");

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel5.setText("Titulo");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(124, 124, 124)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(31, 31, 31)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel4))
                                .addGap(31, 31, 31)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtapellido, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(txtnombre, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(txtcedula, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(txttitulo)))))
                .addContainerGap(140, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtcedula, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(13, 13, 13)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtnombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(txtapellido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(txttitulo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jInternalFrame1.getContentPane().add(jPanel1);
        jPanel1.setBounds(40, 140, 357, 187);

        label1.setText("Numero de Seccion");
        jInternalFrame1.getContentPane().add(label1);
        label1.setBounds(30, 30, 126, 19);

        btncancelar.setText("Cancelar");
        btncancelar.setToolTipText("Cancelar");
        btncancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btncancelarActionPerformed(evt);
            }
        });
        jInternalFrame1.getContentPane().add(btncancelar);
        btncancelar.setBounds(170, 340, 120, 30);

        label2.setText("Promedio");
        jInternalFrame1.getContentPane().add(label2);
        label2.setBounds(30, 80, 63, 19);
        jInternalFrame1.getContentPane().add(txtnumero);
        txtnumero.setBounds(170, 30, 140, 28);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jInternalFrame1, javax.swing.GroupLayout.DEFAULT_SIZE, 428, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jInternalFrame1, javax.swing.GroupLayout.PREFERRED_SIZE, 425, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(20, Short.MAX_VALUE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-454)/2, (screenSize.height-484)/2, 454, 484);
    }

    private void btnaceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnaceptarActionPerformed
        
        if(seccion.getNumseccion()==-1)
            if(seccion.getProf()==null)
                if(validarVacios())
                    if(validarCedula())
                        if(validarNumero()){
                            seccion.setNumseccion(Integer.parseInt(txtnumero.getText()));
                            seccion.setProf(new Profesor(Long.parseLong(txtcedula.getText()), txtnombre.getText(),  txtapellido.getText(),  txttitulo.getText()));
                            JOptionPane.showMessageDialog(new JFrame(),"La seccion fue creada","Aceptar",JOptionPane.INFORMATION_MESSAGE);
                            reiniciar();
                            setVisible(false);
                            Pantalla2 p=new Pantalla2();
                            p.setModalExclusionType(ModalExclusionType.NO_EXCLUDE);
                            p.setSeccion(seccion);
                            p.show();

                        } else
                            JOptionPane.showMessageDialog(new JFrame(),"Numero de seccion invalido","Aceptar",JOptionPane.WARNING_MESSAGE);
                    else
                        JOptionPane.showMessageDialog(new JFrame(),"Numero de cedula invalido","Aceptar",JOptionPane.WARNING_MESSAGE);
                else
                    JOptionPane.showMessageDialog(new JFrame(),"Campos vacios","Aceptar",JOptionPane.WARNING_MESSAGE);
            else
                JOptionPane.showMessageDialog(new JFrame(),"El profesor ya a sido asignado","Aceptar",JOptionPane.WARNING_MESSAGE);
        else
            JOptionPane.showMessageDialog(new JFrame(),"La seccion ya fue creada","Aceptar",JOptionPane.WARNING_MESSAGE);
}

    private void btnsalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnsalirActionPerformed
       
        dispose();
}

    private void btncancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btncancelarActionPerformed
        
        reiniciar();
}

   
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Pantalla1().setVisible(true);
            }
        });
    }

   
    private javax.swing.JButton btnaceptar;
    private javax.swing.JButton btncancelar;
    private javax.swing.JButton btnsalir;
    private javax.swing.JInternalFrame jInternalFrame1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private java.awt.Label label1;
    private java.awt.Label label2;
    private java.awt.Label lblpromedio;
    private javax.swing.JTextField txtapellido;
    private javax.swing.JTextField txtcedula;
    private javax.swing.JTextField txtnombre;
    private javax.swing.JTextField txtnumero;
    private javax.swing.JTextField txttitulo;
   

}
